var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
var startX = 0, endX = 0, prevScrollLeft = 0, prevId = 0, prevIndex = 0;
var px = 0, py = 0, px2 = 0, py2 = 0, windowWidth = 0;
Page({
  data: {
    hasData: false,
    middle_height: "100vh",
    delivery_date: util.dateObjectformat(new Date(), 'yyyy-MM-dd'),
    delivery_date_text: "今天运单",
    delivery_data: [],
    delivery_data_classify:[],
    tab: 3,
    warehouse: '全部项目',
    warehouseId: ''
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onShow: function (e) {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 10),
            warehouseId: res.data.value
          });
          that.loadData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
    wx.getStorage({
      key: 'path_vehicle',
      success: function (res) {
        if (res.data) {
          var vehicleData = res.data;
          wx.showLoading({
            title: '派车中...',
          });
          util.putData("publishDelivery", {
            id: that.data.currentId,
            vehicle: vehicleData.ID,
            driver: vehicleData.driver,
            openid: app.globalData.openid,
            'CO_ID': app.globalData.CO_ID,
            'user_id': app.globalData.user_id
          }, function (res, error) {
            console.log(res); console.log(error)
            wx.hideLoading();
            if (error) {
              wx.showToast({
                title: res.data.data,
                image: '/images/wrong.png',
                duration: 3000
              });
              return false;
            }
            if (res.data.success) {
              wx.showToast({
                title: '派车成功',
                icon: 'success',
                duration: 2000,
                success: function () {
                  setTimeout(function () {
                    that.loadData();
                  }, 2000);
                }
              });
            }
          });
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'path_vehicle',
          success: function (res) {
          }
        });
      }
    });
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--派车" });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 90,
          detail_height: res.windowHeight - 40
        });
        windowWidth = res.windowWidth;
      }
    });
    if (e.warehouseId) {
      that.setData({
        warehouse: e.warehouse,
        warehouseId: e.warehouseId
      });
    }
    that.loadData();
  },
  loadData: function () {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("/atapi/delivery/process/deliveryNonVehicle", {
      CO_ID: app.globalData.CO_ID,
      delivery_date: that.data.delivery_date,
      warehouseId: that.data.warehouseId
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      setTimeout(function(){
        wx.hideLoading();
      },100)
      if (res.data.success) {
        that.setData({
          delivery_data: res.data.data,
          delivery_data_classify: res.data.whData
        })
      }else{
        that.setData({
          delivery_data: [],
          delivery_data_classify: []
        })
      }
    });

  },
  dispatchVehicle:function(e){
    var that = this;
    var id = e.currentTarget.dataset.id;
    that.setData({
      currentId:id
    })
    wx.navigateTo({
      url: '/pages/index/pathSelectVehicle?id=' + id + '&tags=' + e.currentTarget.dataset.tags
    })
  },
  changeDeliveryDate: function (e) {
    this.setData({
      delivery_date: e.detail.value,
      delivery_date_text: e.detail.value
    });
    this.loadData();
  },
  changeWarehouse: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse'
    })
  },
  gotoHome: function () {
    wx.redirectTo({
      url: '/pages/schedule/schedule'
    })
  },
  gotoPathNew: function () {
    wx.redirectTo({
      url: '/pages/index/deliveryForm'
    })
  },
  gotoMonitor: function () {
    wx.redirectTo({
      url: '/pages/schedule/monitor'
    })
  },
  gotoPath: function () {
    wx.redirectTo({
      url: '/pages/schedule/path'
    })
  },
  gotoAlarmMsg: function () {
    wx.redirectTo({
      url: '/pages/schedule/alarmMessage'
    })
  },
  gotoInfo: function () {
    wx.redirectTo({
      url: '/pages/schedule/apps'
    })
  }
})
